package com.jdz.gxq.info.admin.project.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.jdz.gxq.info.admin.project.bo.SignedProject;
import com.jdz.gxq.info.admin.project.bo.TalkingProject;
import com.jdz.gxq.info.admin.project.form.SignedProjectForm;
import com.jdz.gxq.info.admin.project.service.SignedProjectService;
import com.jdz.gxq.info.admin.project.service.TalkingProjectService;
import com.jdz.gxq.info.common.action.CommonDispatchAction;
import com.jdz.gxq.info.common.bo.PageBean;

public class SignedProjectAction extends CommonDispatchAction {
 
	private SignedProjectService signedProjectService = null;
	
	private TalkingProjectService talkingProjectService = null;
	
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		SignedProjectForm signedProjectForm = (SignedProjectForm) form;
		
		String projectName = signedProjectForm.getProjectName();

		int totalCount = signedProjectService.getSignedProjectsCount(projectName);
		
		int currentPage = signedProjectForm.getPageIndex();

		PageBean pageBean = new PageBean(totalCount, currentPage);
		
		signedProjectForm.setPageIndex(pageBean.getCurrentPage());
		
		Map<String, Object> params = new HashMap<String, Object>();
		
		// 分页用
		params.put("startRow", pageBean.getPageStartRow() - 1);
		params.put("rowSize", pageBean.getPageSize());
		params.put("projectName", projectName);
		
		List<SignedProject> signedProjectList = signedProjectService.getSignedProjects(params);
		
		request.setAttribute("signedProjectList", signedProjectList);
		request.setAttribute("pageBean", pageBean);
		
		return mapping.findForward("success");
	}
	
	public ActionForward gotoAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		String projectId = request.getParameter("projectId");
		
		TalkingProject project = talkingProjectService.getTalkingProjectById(Integer.parseInt(projectId));
		
		request.setAttribute("talkingProject", project);
		
		return mapping.findForward("success");
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		SignedProjectForm signedProjectForm = (SignedProjectForm) form;
		
		SignedProject signedProject = signedProjectForm.getSignedProject();
		
		signedProject.setModifiedBy(getUserId(request));
		
		signedProjectService.insertSignedProject(signedProject);

		return mapping.findForward("success");
	}
	
	public ActionForward gotoEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		SignedProjectForm signedProjectForm = (SignedProjectForm) form;
	
		SignedProject signedProject = signedProjectService.getSignedProjectById(signedProjectForm.getId());
		
		signedProjectForm.setSignedProject(signedProject);

		return mapping.findForward("success");
	}
	
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		SignedProjectForm signedProjectForm = (SignedProjectForm) form;
		
		SignedProject signedProject = signedProjectForm.getSignedProject();
		
		signedProject.setModifiedBy(getUserId(request));
		
		signedProjectService.updateSignedProject(signedProject);
		
		return mapping.findForward("success");
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		String projectIdsStr = request.getParameter("id");
		
		String[] projectIds = projectIdsStr.split(",");
		
		for (String projectId : projectIds) {
			
			SignedProject signedProject = new SignedProject();
			signedProject.setProjectId(Integer.parseInt(projectId));
			signedProject.setProjectStatus("1");
			signedProject.setModifiedBy(getUserId(request));
			
			signedProjectService.deleteSignedProject(signedProject);
		}

		return mapping.findForward("success");
	}
	
	public void setSignedProjectService(SignedProjectService signedProjectService) {
		this.signedProjectService = signedProjectService;
	}

	public void setTalkingProjectService(TalkingProjectService talkingProjectService) {
		this.talkingProjectService = talkingProjectService;
	}
}

